<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="cancel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <!-- 选项 -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- {{ checkList }} -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOK">提交</el-button>
        <el-button size="small" @click="cancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assginRole } from '@/api/employess'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checkList: [],
      list: [],
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    // 获取角色列表
    async  getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 999 })
      this.list = rows
    },
    async getUserDetailById(id) {
      this.userId = id
      const res = await getUserDetailById(id)
      //   console.log(res)
      this.checkList = res.roleIds
    },
    cancel() {
      this.$emit('update:showRoleDialog', false)
    },
    async  btnOK() {
      // 发送请求
      await assginRole({
        id: this.userId,
        roleIds: this.checkList
      })
      // 提示成功
      this.$message.success('修改成功')
      // 关闭弹窗
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>

<style>

</style>
